<template>
	<view class="u-wrap">
		<view class="u-search-box">
			<u-search placeholder="日照香炉生紫烟" v-model="keyword" @custom="initData" @clear="initData"></u-search>
		</view>
		<view class="u-menu-wrap">
			<scroll-view scroll-y scroll-with-animation class="u-tab-view menu-scroll-view">
				<block v-for="(item,index) in categories" :key="index">
					<view v-for="subItem in item.children" :key="subItem.id" class="u-tab-item"
						:class="[current == subItem.id ? 'u-tab-item-active' : '']" :data-current="subItem.id" @tap.stop="swichMenu(subItem.id)">
						<text class="u-line-1">{{subItem.name}}</text>
					</view>
				</block>
			</scroll-view>
			<scroll-view scroll-y class="right-box" @scrolltolower="scrollEvent($event)">
				<view class="page-view">
					<view class="class-item">
						<view class="item-container">
							<navigator :url="`/pages/goods/show?id=${goods.id}`" class="thumb-box u-p-b-40 u-p-t-40" v-for="goods in goodsList" :key="goods.id">
								<image class="item-menu-image" :src="goods.cover_url"></image>
								<view class="item-menu-name">{{goods.title}}</view>
							</navigator>
							<view v-if="!goodsList.length" class="u-flex-1 u-p-t-80">
								<u-empty text="暂无内容" mode="list"></u-empty>
							</view>
						</view>
					</view>
				</view>
			</scroll-view>
		</view>
	</view>
</template>
<script>
	export default {
		data() {
			return {
				keyword: '',
				current: 0, // 预设当前项的值
				goodsList: [],
				page: 1,
				categories: [],
				isLast: false,
				scrollTop: 0,
			}
		},
		onLoad() {
			this.getData();
		},
		methods: {
			async getData() {
				const params = { page: this.page, title: this.keyword };
				if (this.current) params.category_id = this.current;
				const { categories, goods, recommend_goods } = await this.$u.api.getGoods(params);
				this.categories = categories
				this.goodsList = [...this.goodsList, ...goods.data];
				this.isLast = goods.next_page_url ? false : true;
			},
			// 点击左边的栏目切换
			async swichMenu(id) {
				if (id === this.current) return;
				this.current = id;
				this.initData();
			},
			async scrollEvent(e) {
				if (this.isLast) return this.$u.toast('已经是最后一页了')
				this.page += 1;
				this.getData();
			},
			initData() {
				this.goodsList = [];
				this.page = 1;
				this.getData();
			}
		}
	}
</script>

<style lang="scss" scoped>
	.u-wrap {
		height: calc(100vh);
		/* #ifdef H5 */
		height: calc(100vh - var(--window-top));
		/* #endif */
		display: flex;
		flex-direction: column;
	}

	.u-search-box {
		padding: 18rpx 30rpx;
	}

	.u-menu-wrap {
		flex: 1;
		display: flex;
		overflow: hidden;
	}

	.u-search-inner {
		background-color: rgb(234, 234, 234);
		border-radius: 100rpx;
		display: flex;
		align-items: center;
		padding: 10rpx 16rpx;
	}

	.u-search-text {
		font-size: 26rpx;
		color: $u-tips-color;
		margin-left: 10rpx;
	}

	.u-tab-view {
		width: 200rpx;
		height: 100%;
	}

	.u-tab-item {
		height: 110rpx;
		background: #f6f6f6;
		box-sizing: border-box;
		display: flex;
		align-items: center;
		justify-content: center;
		font-size: 26rpx;
		color: #444;
		font-weight: 400;
		line-height: 1;
	}

	.u-tab-item-active {
		position: relative;
		color: #000;
		font-size: 30rpx;
		font-weight: 600;
		background: #fff;
	}

	.u-tab-item-active::before {
		content: "";
		position: absolute;
		border-left: 4px solid $u-type-primary;
		height: 32rpx;
		left: 0;
		top: 39rpx;
	}

	.u-tab-view {
		height: 100%;
	}

	.right-box {
		background-color: rgb(250, 250, 250);
	}

	.page-view {
		padding: 16rpx;
	}

	.class-item {
		margin-bottom: 30rpx;
		background-color: #fff;
		padding: 16rpx;
		border-radius: 8rpx;
	}

	.class-item:last-child {
		min-height: 100vh;
	}

	.item-title {
		font-size: 26rpx;
		color: $u-main-color;
		font-weight: bold;
	}

	.item-menu-name {
		font-weight: normal;
		font-size: 24rpx;
		color: $u-main-color;
	}

	.item-container {
		display: flex;
		flex-wrap: wrap;
	}

	.thumb-box {
		width: 49%;
		display: flex;
		align-items: center;
		justify-content: center;
		flex-direction: column;
		margin-top: 20rpx;
	}

	.item-menu-image {
		width: 120rpx;
		height: 120rpx;
	}
</style>
